<template>
  <div class="common-layout">
    <el-container>
      <el-aside width="220px" class="sidebar">
        <div class="logo">
          <h3>学生管理系统</h3>
        </div>
        <el-menu
          :default-active="activeMenu"
          active-text-color="#409eff"
          background-color="#ffffff"
          class="el-menu-vertical-demo"
          text-color="#333333"
          router
          unique-opened
          @open="handleOpen"
          @close="handleClose"
        >
          <el-menu-item index="/">
            <el-icon><House /></el-icon>
            <span>首页</span>
          </el-menu-item>
          <el-sub-menu index="student">
            <template #title>
              <el-icon><User /></el-icon>
              <span>学生管理</span>
            </template>
            <el-menu-item index="/studentselect">
              <el-icon><Search /></el-icon>
              <span>学生查询</span>
            </el-menu-item>
            <el-menu-item index="/studententry">
              <el-icon><Plus /></el-icon>
              <span>学生录入</span>
            </el-menu-item>
          </el-sub-menu>
          <el-menu-item index="/log">
            <el-icon><Document /></el-icon>
            <span>日志管理</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      <el-container>
        <el-header class="header">
          <div class="header-content">
            <h2>学生管理系统</h2>
            <div class="user-info">
              <el-dropdown>
                <span class="el-dropdown-link">
                  <el-avatar size="small" icon="UserFilled" />
                  管理员
                  <el-icon class="el-icon--right">
                    <arrow-down />
                  </el-icon>
                </span>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item @click="showProfile">个人信息</el-dropdown-item>
                    <el-dropdown-item @click="showSettings">设置</el-dropdown-item>
                    <el-dropdown-item divided @click="logout">退出登录</el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </div>
          </div>
        </el-header>
        <el-main class="main-content">
          <RouterView />
        </el-main>
        <el-footer class="footer">
          © 2025 学生管理系统 - 一个基于Vue 3和Element Plus的演示项目
        </el-footer>
      </el-container>
    </el-container>
  </div>
</template>

<script lang="ts" setup>
import {
  House,
  User,
  Search,
  Plus,
  Document,
  ArrowDown
} from '@element-plus/icons-vue'
import { useRoute } from 'vue-router'
import { computed } from 'vue'
import { ElMessageBox, ElMessage } from 'element-plus'

const route = useRoute()

const activeMenu = computed(() => {
  const { path } = route
  return path
})

const handleOpen = (key: string, keyPath: string[]) => {
  console.log(key, keyPath)
}

const handleClose = (key: string, keyPath: string[]) => {
  console.log(key, keyPath)
}

// 显示个人信息
const showProfile = () => {
  ElMessage.info('此功能将在后续版本中实现')
}

// 显示设置
const showSettings = () => {
  ElMessage.info('此功能将在后续版本中实现')
}

// 退出登录
const logout = () => {
  ElMessageBox.confirm(
    '确定要退出登录吗？',
    '确认退出',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
  .then(() => {
    // 实际项目中这里需要调用退出登录的API
    ElMessage({
      type: 'success',
      message: '退出登录成功',
    })
    // 跳转到登录页面，这里简化处理，实际项目中可能需要使用router.push
    window.location.reload()
  })
  .catch(() => {
    ElMessage({
      type: 'info',
      message: '已取消退出',
    })
  })
}
</script>

<style scoped>
.common-layout {
  height: 100vh;
  width: 100vw;
  position: fixed;
  top: 0;
  left: 0;
}

.el-container {
  height: 100%;
}

.sidebar {
  background-color: #ffffff;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
  z-index: 100;
  height: 100%;
  overflow-y: auto;
}

.logo {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-bottom: 1px solid #eee;
}

.logo h3 {
  color: #409eff;
  margin: 0;
}

.header {
  background-color: #ffffff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  padding: 0;
  height: 60px;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  padding: 0 20px;
}

.header-content h2 {
  margin: 0;
  color: #333;
  font-size: 20px;
}

.user-info {
  display: flex;
  align-items: center;
}

.el-dropdown-link {
  cursor: pointer;
  display: flex;
  align-items: center;
  color: #333;
}

.el-dropdown-link:focus {
  outline: none;
}

.main-content {
  background-color: #f5f5f5;
  padding: 20px;
  overflow-y: auto;
  height: calc(100% - 60px - 50px); /* 总高度 - header高度 - footer高度 */
}

.footer {
  background-color: #ffffff;
  border-top: 1px solid #eee;
  text-align: center;
  color: #666;
  font-size: 14px;
  height: 50px;
  line-height: 50px;
  padding: 0;
}
</style>